﻿using Newtonsoft.Json;
using System;
using System.IO;

namespace LdtJudge.DataEngine.CallInterface.ConfigLog
{
    public enum MessageType
    {
        /// <summary>
        /// The error
        /// </summary>
        Error,
        /// <summary>
        /// The information
        /// </summary>
        Info,
        ///// <summary>
        ///// The debug
        ///// </summary>
        //Debug,
        ///// <summary>
        ///// The fatal
        ///// </summary>
        //Fatal,
        ///// <summary>
        ///// The trace
        ///// </summary>
        //Trace,
        ///// <summary>
        ///// The warn
        ///// </summary>ss 
        //Warn
    }

    public class LogFileWriteMessage
    {
        private static string BaseFilePath = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 调用接口日志输出
        /// DateTime.Now.ToString("yyyyMMddHH") + ".log";
        /// </summary>
        private static string filename
        {
            get => DateTime.Now.ToString("yyyyMMddHH") + ".log";
        }
        public static object ob = new object();

        public LogFileWriteMessage()
        {

        }

        /// <summary>
        /// 写入流转日志消息
        /// </summary>
        /// <param name="message"></param>
        public static void WriteLogs(string message)
        {
            WriteLogs(MessageType.Info, message);
        }

       
       
        /// <summary>
        /// 写入日志内容
        /// </summary>
        /// <param name="messageType"></param>
        /// <param name="message"></param>
        public static void WriteLogs(MessageType messageType, string message)
        {
            string path = BaseFilePath + @"LogCallInterface";
            if (!Directory.Exists(path))
            {//如果文件夹不存则进行创建
                Directory.CreateDirectory(path);
            }
            string messages = string.Empty;
            switch (messageType)
            {

                case MessageType.Error:
                    messages = "XXX  错误消息为：" + message;
                    break;
                case MessageType.Info:
                    messages = "！！！流转消息为：" + message;
                    break;
                    //case MessageType.Trace:
                    //    messages = "" + message;
                    //    break;
                    //case MessageType.Warn:
                    //    messages = "" + message;
                    //    break;
                    //case MessageType.Debug:
                    //    messages = "" + message;
                    //    break;
                    //case MessageType.Fatal:
                    //    messages = "" + message;
                    //    break;
            }
            lock (ob)
            {
                byte[] myByte = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString() + "\r\n" + messages + "\r\n");
                using (FileStream fsWrite = new FileStream(path + @"\" + filename, FileMode.Append, FileAccess.Write))
                {
                    fsWrite.Write(myByte, 0, myByte.Length);
                }
            }

        }


        /// <summary>
        /// 将文件对象进行用JSON转换为String字符串
        /// Returns the json string.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>System.String.</returns>
        public static string ReturnJsonString(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }


    }
}
